﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Web;
using TrafficArchives.Common;

namespace TrafficArchives.DataAccess
{
    /// <summary>
    /// 角色管理数据库操作。
    /// </summary>
    public class RoleDb : DbBase
    {
        /// <summary>
        /// 读取角色数据表。
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override PaginationData<DataTable> GetData(Request request)
        {
            var paginationData = new PaginationData<DataTable>();
            var normalSql = "select t.*, rownum as rowno from v_role t where " + request.ToSql();
            var sql = request.Pagination.Enabled
                          ? string.Format("select * from ({0}) A where A.rowno > {1} and A.rowno <= {2}", normalSql,
                                          GetPageStart(request.Pagination), GetPageEnd(request.Pagination))
                          : normalSql;
            paginationData.Data = ExecuteDataTable(sql);
            if (request.Pagination.Enabled)
            {
                paginationData.RecordCount = ExecuteCount(normalSql.Replace("t.*, rownum as rowno", "count(*)"));
            }
            return paginationData;
        }

        /// <summary>
        /// 根据Id取单位数据。
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override DataTable GetData(string id)
        {
            var request = new Request();
            request.Items.Add(new Condition
            {
                FieldName = "USERROLE_ID",
                Operator = ConditionOperator.Equal,
                Values = new object[] { id }
            });
            return GetData(request).Data;
        }

        /// <summary>
        /// 取表结构。
        /// </summary>
        /// <returns></returns>
        public override DataTable GetNoneData()
        {
            var request = new Request();
            request.Items.Add(new Condition
            {
                FieldName = "USERROLE_ID",
                Operator = ConditionOperator.Equal,
                Values = new object[] { "null" }
            });
            return GetData(request).Data;
        }

        public override void Add(DataTable data)
        {
            if (data.Rows.Count == 0)
            {
                return;
            }
            const string sql = "insert into USERROLETABLE(USERROLE_ID,ROLENAME,POWERASSEMBLE,CREATETIME,ISVALIS)" +
                               "values(:USERROLE_ID,:ROLENAME,:POWERASSEMBLE,:CREATETIME,:ISVALIS)";
            var parameters = new[]{
                                    MakeParam(":USERROLE_ID", OracleType.VarChar, 40, "USERROLE_ID"),
                                     MakeParam(":ROLENAME", OracleType.VarChar, 40, "ROLENAME"),
                                     MakeParam(":POWERASSEMBLE", OracleType.VarChar, 1000, "POWERASSEMBLE"),
                                     MakeParam(":CREATETIME", OracleType.DateTime, 40, "CREATETIME"),
                                     MakeParam(":ISVALIS", OracleType.Char, 1, "ISVALIS")
                                  };
            OnAdd(data, sql, parameters);
        }

        /// <summary>
        /// 修改角色数据。
        /// </summary>
        /// <param name="data"></param>
        public override void Update(DataTable data)
        {
            if (data.Rows.Count == 0)
            {
                return;
            }
            const string sql = "update USERROLETABLE set " +
                               "ROLENAME = :ROLENAME," +
                               "POWERASSEMBLE = :POWERASSEMBLE," +
                               "CREATETIME = :CREATETIME," +
                               "ISVALIS = :ISVALIS where USERROLE_ID = :USERROLE_ID";
            var parameters = new[]{
                                     MakeParam(":ROLENAME", OracleType.VarChar, 40, "ROLENAME"),
                                     MakeParam(":POWERASSEMBLE", OracleType.VarChar, 1000, "POWERASSEMBLE"),
                                     MakeParam(":CREATETIME", OracleType.DateTime, 40, "CREATETIME"),
                                     MakeParam(":ISVALIS", OracleType.Char, 1, "ISVALIS"),
                                     MakeParam(":USERROLE_ID", OracleType.VarChar, 40, "USERROLE_ID")
                                  };
            OnUpdate(data, sql, parameters);
        }

        /// <summary>
        /// 授权菜单。
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="menuString"></param>
        public void Authorize(string roleId, string menuString)
        {
            var data = GetData(roleId);
            if (data.Rows.Count == 0)
            {
                return;
            }
            data.Rows[0]["POWERASSEMBLE"] = menuString;

            Update(data);
        }

        /// <summary>
        /// 设置启用状态。
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="isEnabled"></param>
        public void SetEnable(string roleId, bool isEnabled)
        {
            var data = GetData(roleId);
            if (data.Rows.Count == 0)
            {
                return;
            }
            data.Rows[0]["isvalis"] = isEnabled ? 1 : 0;

            Update(data);
        }

        /// <summary>
        /// 删除角色。
        /// </summary>
        /// <param name="ids"></param>
        public override void Delete(List<string> ids)
        {
            const string sql = "delete from USERROLETABLE t where t.USERROLE_ID in ('{0}')";
            ExecuteNoneQuery(string.Format(sql, string.Join("','", ids)));
        }
    }
}